home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / progs / demos / atlantis / atlantis.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  7KB  |  266 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /**
  5.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  6.  * ALL RIGHTS RESERVED
  7.  * Permission to use, copy, modify, and distribute this software for
  8.  * any purpose and without fee is hereby granted, provided that the above
  9.  * copyright notice appear in all copies and that both the copyright notice
  10.  * and this permission notice appear in supporting documentation, and that
  11.  * the name of Silicon Graphics, Inc. not be used in advertising
  12.  * or publicity pertaining to distribution of the software without specific,
  13.  * written prior permission.
  14.  *
  15.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  16.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  17.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  18.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  19.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  20.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  21.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  22.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  23.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  24.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  25.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  26.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  *
  28.  * US Government Users Restricted Rights
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  31.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  32.  * clause at DFARS 252.227-7013 and/or in similar or successor
  33.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  34.  * Unpublished-- rights reserved under the copyright laws of the
  35.  * United States.  Contractor/manufacturer is Silicon Graphics,
  36.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  37.  *
  38.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  39.  */
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <math.h>
  44. #include <GL/glut.h>
  45. #include "atlantis.h"
  46.  
  47. fishRec sharks[NUM_SHARKS];
  48. fishRec momWhale;
  49. fishRec babyWhale;
  50. fishRec dolph;
  51.  
  52. GLboolean moving;
  53.  
  54. void
  55. InitFishs(void)
  56. {
  57.     int i;
  58.  
  59.     for (i = 0; i < NUM_SHARKS; i++) {
  60.         sharks[i].x = 70000.0 + rand() % 6000;
  61.         sharks[i].y = rand() % 6000;
  62.         sharks[i].z = rand() % 6000;
  63.         sharks[i].psi = rand() % 360 - 180.0;
  64.         sharks[i].v = 1.0;
  65.     }
  66.  
  67.     dolph.x = 30000.0;
  68.     dolph.y = 0.0;
  69.     dolph.z = 6000.0;
  70.     dolph.psi = 90.0;
  71.     dolph.theta = 0.0;
  72.     dolph.v = 3.0;
  73.  
  74.     momWhale.x = 70000.0;
  75.     momWhale.y = 0.0;
  76.     momWhale.z = 0.0;
  77.     momWhale.psi = 90.0;
  78.     momWhale.theta = 0.0;
  79.     momWhale.v = 3.0;
  80.  
  81.     babyWhale.x = 60000.0;
  82.     babyWhale.y = -2000.0;
  83.     babyWhale.z = -2000.0;
  84.     babyWhale.psi = 90.0;
  85.     babyWhale.theta = 0.0;
  86.     babyWhale.v = 3.0;
  87. }
  88.  
  89. void
  90. Init(void)
  91. {
  92.     static float ambient[] =
  93.     {0.1, 0.1, 0.1, 1.0};
  94.     static float diffuse[] =
  95.     {1.0, 1.0, 1.0, 1.0};
  96.     static float position[] =
  97.     {0.0, 1.0, 0.0, 0.0};
  98.     static float mat_shininess[] =
  99.     {90.0};
  100.     static float mat_specular[] =
  101.     {0.8, 0.8, 0.8, 1.0};
  102.     static float mat_diffuse[] =
  103.     {0.46, 0.66, 0.795, 1.0};
  104.     static float mat_ambient[] =
  105.     {0.0, 0.1, 0.2, 1.0};
  106.     static float lmodel_ambient[] =
  107.     {0.4, 0.4, 0.4, 1.0};
  108.     static float lmodel_localviewer[] =
  109.     {0.0};
  110.     GLfloat map1[4] =
  111.     {0.0, 0.0, 0.0, 0.0};
  112.     GLfloat map2[4] =
  113.     {0.0, 0.0, 0.0, 0.0};
  114.  
  115.     glFrontFace(GL_CW);
  116.  
  117.     glDepthFunc(GL_LEQUAL);
  118.     glEnable(GL_DEPTH_TEST);
  119.  
  120.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  121.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  122.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  123.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  124.     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
  125.     glEnable(GL_LIGHTING);
  126.     glEnable(GL_LIGHT0);
  127.  
  128.     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  129.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  130.     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
  131.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
  132.  
  133.     InitFishs();
  134.  
  135.     glClearColor(0.0, 0.5, 0.9, 0.0);
  136. }
  137.  
  138. void
  139. Reshape(int width, int height)
  140. {
  141.     glViewport(0, 0, width, height);
  142.  
  143.     glMatrixMode(GL_PROJECTION);
  144.     glLoadIdentity();
  145.     gluPerspective(400.0, 2.0, 1.0, 2000000.0);
  146.     glMatrixMode(GL_MODELVIEW);
  147. }
  148.  
  149. void
  150. Animate(void)
  151. {
  152.     int i;
  153.  
  154.     for (i = 0; i < NUM_SHARKS; i++) {
  155.         SharkPilot(&sharks[i]);
  156.         SharkMiss(i);
  157.     }
  158.     WhalePilot(&dolph);
  159.     dolph.phi++;
  160.     glutPostRedisplay();
  161.     WhalePilot(&momWhale);
  162.     momWhale.phi++;
  163.     WhalePilot(&babyWhale);
  164.     babyWhale.phi++;
  165. }
  166.  
  167. void
  168. Key(unsigned char key, int x, int y)
  169. {
  170.     switch (key) {
  171.     case 27:           /* Esc will quit */
  172.         exit(1);
  173.         break;
  174.     case ' ':          /* space will advance frame */
  175.         if (!moving) {
  176.             Animate();
  177.         }
  178.     }
  179. }
  180.  
  181. void
  182. Display(void)
  183. {
  184.     int i;
  185.  
  186.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  187.  
  188.     for (i = 0; i < NUM_SHARKS; i++) {
  189.         glPushMatrix();
  190.         FishTransform(&sharks[i]);
  191.         DrawShark(&sharks[i]);
  192.         glPopMatrix();
  193.     }
  194.  
  195.     glPushMatrix();
  196.     FishTransform(&dolph);
  197.     DrawDolphin(&dolph);
  198.     glPopMatrix();
  199.  
  200.     glPushMatrix();
  201.     FishTransform(&momWhale);
  202.     DrawWhale(&momWhale);
  203.     glPopMatrix();
  204.  
  205.     glPushMatrix();
  206.     FishTransform(&babyWhale);
  207.     glScalef(0.45, 0.45, 0.3);
  208.     DrawWhale(&babyWhale);
  209.     glPopMatrix();
  210.  
  211.     glutSwapBuffers();
  212. }
  213.  
  214. void
  215. Visible(int state)
  216. {
  217.     if (state == GLUT_VISIBLE) {
  218.         if (moving)
  219.             glutIdleFunc(Animate);
  220.     } else {
  221.         if (moving)
  222.             glutIdleFunc(NULL);
  223.     }
  224. }
  225.  
  226. void
  227. menuSelect(int value)
  228. {
  229.     switch (value) {
  230.     case 1:
  231.         moving = GL_TRUE;
  232.         glutIdleFunc(Animate);
  233.         break;
  234.     case 2:
  235.         moving = GL_FALSE;;
  236.         glutIdleFunc(NULL);
  237.         break;
  238.     case 3:
  239.         exit(0);
  240.         break;
  241.     }
  242. }
  243.  
  244. int
  245. main(int argc, char **argv)
  246. {
  247.     glutInitWindowSize(500, 250);
  248.     glutInit(&argc, argv);
  249.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  250.     glutCreateWindow("GLUT Atlantis Demo");
  251.     Init();
  252.     glutDisplayFunc(Display);
  253.     glutReshapeFunc(Reshape);
  254.     glutKeyboardFunc(Key);
  255.     moving = GL_TRUE;
  256.     glutIdleFunc(Animate);
  257.     glutVisibilityFunc(Visible);
  258.     glutCreateMenu(menuSelect);
  259.     glutAddMenuEntry("Start motion", 1);
  260.     glutAddMenuEntry("Stop motion", 2);
  261.     glutAddMenuEntry("Quit", 3);
  262.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  263.     glutMainLoop();
  264.     return 0;             /* ANSI C requires main to return int. */
  265. }
  266.